home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!cronkite!news
- From: Marco DeFreitas <mdefreitas@sikorsky.com>
- Subject: Re: Virtuals in constructor
- Content-Type: text/plain; charset=us-ascii
- Message-ID: <1996Feb21.011424.21712@cronkite.res.utc.com>
- Sender: news@cronkite.res.utc.com
- Nntp-Posting-Host: iris604.asi.sikorsky.com
- Content-Transfer-Encoding: 7bit
- Organization: Sikorsky Aircraft
- References: <312A3A72.688D@scopus.ch>
- Mime-Version: 1.0
- Date: Wed, 21 Feb 1996 01:14:24 GMT
- X-Mailer: Mozilla 1.1S (X11; I; IRIX 5.3 IP12)
- X-Url: news:312A3A72.688D@scopus.ch
-
- Laurent,
-
- It looks as though your pure virtual function should be called from the
- class that instanced it. BTW, it typically is not safe to call a derived
- class fuction from the base class constructor, since the derived class
- function may used derived members (which haven't been created yet, since
- you are still in the base class). Try the following, hope it helps:
-
-
- class A
- {
- public:
- A(VOID) {}
-
- virtual VOID Method(VOID) = 0;
- };
-
- class B : public A
- {
- public:
- B(VOID) {Method();}
-
- VOID Method(VOID) { MessageBox(NULL, "", "", MB_OK); }
- };
-
- int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
- {
- B b;
-
- return 0;
- }
-
-
- -------
- Regards,
- Sikorsky Aircraft
- Stratford, CT
-
- >
- Hi,
- >
- >does anyone know why the following code doesn't work ?
- >
- >Run-time error: pure virtual function called
- >
- >class A
- >{
- > public:
- > A(VOID) { Method(); }
- >
- > virtual VOID Method(VOID) = 0;
- >};
- >
- >class B : public A
- >{
- > public:
- > B(VOID) {}
- >
- > VOID Method(VOID) { MessageBox(NULL, "", "", MB_OK); }
- >};
- >
- >int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
- >{
- > B b;
- >
- > return 0;
- >}
- >
- >Thanks
- >
- > Laurent Guinnard
- > guinnard@eig.unige.ch
- >
- >be_well++;
-
-